<?php
/*
+ --------------------------------------------------- +
|  Ineo Article Manager - Version 2.x
|  Copyright 2006-2007, Dan Cryer and William McGann
+ --------------------------------------------------- +

Ineo Article Manager is free software; you can redistribute it and/or modify
it under the terms of version 3 of the GNU General Public License as published by
the Free Software Foundation.

Ineo Article Manager is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Ineo; see the file License.txt.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

*/


class overview_plugin_statistics
{
	// ineo core
	var $core;
	// ipsclass
	var $ipsclass;
	
	// plugin data:
	var $plugin_data = array( 'tab_title' => 'Statistics', 'is_tab' => false, 'tab_order' => 99, 'enabled' => true );
		
	/**
	* Constructor - Set variables, load libraries, etc.
	*/
	function overview_plugin_statistics( &$core )
	{
		// Set variables:
		$this->core     =& $core;
		$this->ipsclass =& $this->core->ipsclass;
		
		// Set lang-friendly title:
		$this->plugin_data['tab_title'] = $this->ipsclass->lang['ineo_optab_statistics'];
	}
	
	/**
	* Return plugin data to the core 
	*/
	function get_data()
	{
		// Load Sessions, Whee!
		$this->ipsclass->load_language('lang_boards');
		
		$time = time() - ( $this->ipsclass->vars['au_cutoff'] * 60 );
		
		$this->ipsclass->DB->simple_construct(
									array('select' => 's.*,g.prefix,g.suffix',
										  'from'   => array( 'sessions' => 's' ),
										  'where'  => 'location = \'mod:ineo\' AND running_time > ' . $time . ' AND member_id != ' . $this->ipsclass->member['id'],
										  'add_join' => array( 1 => 
										  						array(	'type' => 'left',
										  					  			'from' => array('groups' => 'g'), 
										  					  			'where' => 'g.g_id=s.member_group',
										  					  		 )
										  					  ),
										  )
										     );
										     
		$this->ipsclass->DB->simple_order( 'running_time DESC' );
		$squery = $this->ipsclass->DB->simple_exec();
		
		//----
		// Create the array we'll pass to the template:
		//----
		$sess_data = array(
							'members' => 0,
							'guests'  => 0,
							'anon'    => 0,
							'list'    => '',
						  );
						  
		// Member name cache ( no double printing ):
		$cached = array();
				
		$ineo_sessions = array();
		
		if ( $this->ipsclass->member['id'] != 0 )
		{
			$anon = ($this->ipsclass->member['login_anonymous'] == '0&1') ? 0 : 1;
			
			$ineo_sessions[ md5( $this->ipsclass->member['id'] ) ] = array( 
								'login_type'   => $anon,
								'member_id'    => $this->ipsclass->member['id'],
								'member_name'  => $this->ipsclass->member['members_display_name'],
								'member_group' => $this->ipsclass->member['mgroup'],
								'location_1_type' => 'idx',
								'running_time' => time(),
								'prefix' => $this->ipsclass->member['prefix'],
								'suffix' => $this->ipsclass->member['suffix'],
								);
		}
		else
		{			
			$ineo_sessions[ md5(uniqid('ineo')) ] = array( 
								'login_type'   => 0,
								'running_time' => time(),
								'member_id'    => 0,
								'member_name'  => 'Guest',
								'member_group' => $this->ipsclass->member['mgroup'],
								'location_1_type' => $this->ipsclass->input['op'],
								'prefix' => $this->ipsclass->member['prefix'],
								'suffix' => $this->ipsclass->member['suffix'],
								);
		}
		
		while( $session = $this->ipsclass->DB->fetch_row( $squery ) )
		{
			$ineo_sessions[$session['id']] = $session;
		}
								
		//----
		// Populate the array:
		//----
			
		foreach( $ineo_sessions as $session )
		{
				
			if( $session['member_id'] == 0 )
			{
				// Guest:
				$sess_data['guests']++;
			}
			else
			{			
				if( !isset( $cached[$session['member_id']] ) )
				{
					// Member:
					if( $session['login_type'] != 1 )
					{
						// Regular member:
						$sess_data['members']++;
						$show = 1;
					}
					else
					{
						// Anonymous:
						$sess_data['anon']++;
						$show = 0;
					}
					
					// Show in list if they are not anon, OR if the active user is a root admin:
					if( $show == 1 or ( $show == 0 && $this->ipsclass->member['g_ineo_ismod'] == 1 ) )
					{
						// Add a comma first, unless it is the first run:
						if( isset( $one )) 
						{ 
							$sess_data['list'] .= ', '; 
						}
						$one = 1;
						
						// Create the 'title=' value:
						$time = $this->ipsclass->get_time( $session['running_time'] );
						
						$action = $this->ipsclass->lang['ineo_act_' . $session['location_1_type']];
						
						$alt = $action . " - " . $time;
						
					
						// Create a list entry:
						$sess_data['list'] .= '<a title=\''.$alt.'\' href=\''.$this->ipsclass->base_url.'showuser=' . $session['member_id'] . '\'>' . $session['prefix'] .  $session['member_name'] . $session['suffix'] . '</a>';
					
						// Add a * for anon user:
						$sess_data['list'] .= ( $show == 0 ) ? '*' : '';
					}
				}
				else
				{
					// User is already in the list, count this one as a guest:
					$sess_data['guests']++;
				}
			}
		}
				
		// Add up all the counts for the total:
		$sess_data['total'] = $sess_data['guests'] + $sess_data['members'] + $sess_data['anon'];
		
		//----
		// Create statistics:
		//----
		
		// Article count:
		$this->ipsclass->DB->simple_construct( array( 'select' => 'count( article_id ) as articles' , 'from' => 'ineo_articles', 'where' => 'article_is_queued = 0' ) );
		$count = $this->ipsclass->DB->simple_exec();
		$count = $this->ipsclass->DB->fetch_row( $count );
		
		$count = $count['articles'];
		$stats['articles'] = $count;
		
		// Get the latest article:
		
		$this->ipsclass->DB->simple_construct(
									array('select' => 'a.* , m.members_display_name',
										  'from'   => array( 'ineo_articles' => 'a' ),
										  'order by' => 'a.article_id DESC',
										  'where' => 'a.article_is_queued = \'0\'',
										  'add_join' => array( 1 => 
										  						array(	'type' => 'left',
										  					  			'from' => array('members' => 'm'), 
										  					  			'where' => 'm.id=a.article_author',
										  					  		 )
										  					  ),
										  )
										     );
										     
		$this->ipsclass->DB->simple_order( 'a.article_id DESC' );
		$this->ipsclass->DB->simple_limit( 1 );									
		
		$article = $this->ipsclass->DB->simple_exec();
		$article = $this->ipsclass->DB->fetch_row($article);
		
		if( !is_numeric($article['article_author']) )
		{
			$article['members_display_name'] = $article['article_author'];
			$article['article_author'] = 0;
		}
		
		$stats['latest_url']   = $this->core->libs['global']->make_article_url( $article['article_id'], $article['article_text_id'] );
		$stats['latest_title'] = $article['article_title'];
		$stats['latest_user']  = $article['members_display_name'];
		$stats['latest_uid']   = $article['article_author'];
		
		//----
		// Send it to the template:
		//----
		
		$tmp = $this->core->templates['global']->users_online( $sess_data , $stats );
		
		// Replace 'Users in the last %s minutes' with 'last 15 minutes' or whatever it is:
				
		return str_replace( '%s' , $this->ipsclass->vars['au_cutoff'] , $tmp );
	}
}

?>